import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import {
  Message
} from 'element-ui'
Vue.use(VueRouter)

const routes = [
  // 登录
  {
    path: '/',
    name: 'login',
    component: () => import('../views/user/LoginView.vue')
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path: '/admin',
    name: 'admin',
    component: () => import('../views/admin/AdminView.vue'),
    meta: {
      requireAuth: true,
    },
    // 定义子路由
    children: [
      // 首页
      {
        path: 'index',
        name: 'index',
        component: () => import("../views/admin/IndexView.vue")
      },
      // 个人中心
      {
        path: 'inof',
        name: 'inof',
        component: () => import("../views/admin/My/MyInof.vue")
      },
      // 下面是《 工作台 》选项里的路由----------------------------------------------------
      // 组件预览
      {
        path: 'pre',
        name: 'pre',
        component: () => import("../views/admin/workbench/PreView.vue")
      },
      // Crud示例
      {
        path: 'Crud',
        name: 'Crud',
        component: () => import("../views/admin/workbench/Component/CrudExample.vue")
      },
      // quill 富文本编辑器
      {
        path: 'quill',
        name: 'quill',
        component: () => import("../views/admin/workbench/Component/QuillRich.vue")
      },
      // 文件上传
      {
        path: 'Rich',
        name: 'Rich',
        component: () => import("../views/admin/workbench/Component/RichTextEditor.vue")
      },
      // 下面是《 系统管理 》选项里的路由----------------------------------------------------

      {
        path: 'user', //用户列表
        name: 'userlist',
        component: () => import("../views/admin/system/power/UserList.vue")
      },
      {
        path: 'menu', //菜单列表
        name: 'menulist',
        component: () => import("../views/admin/system/power/MenuList.vue")
      },
      {
        path: 'role', //角色列表
        name: 'rolelist',
        component: () => import("../views/admin/system/power/RoleList.vue")
      },
      // 请求日志
      {
        path: 'jour',
        name: 'jour',
        component: () => import("../views/admin/system/monitor/journalList.vue")
      },
      // 参数列表
      {
        path: 'parameter',
        name: 'parameter',
        component: () => import("../views/admin/system/parameter/parameList.vue")
      },

      // 下面是《 字典管理 》选项里的路由----------------------------------------------------
      // 字典列表
      {
        path: 'Dicti',
        name: 'Dicti',
        component: () => import("../views/admin/Dictionaries/DictionariesList.vue")
      },

      // 下面是《 框架教程 》选项里的路由----------------------------------------------------
      // 框架教程/文档
      {
        path: 'file',
        name: 'file',
        component: () => import("../views/admin/frame/fileList.vue")
      },

      // 下面是《 任务管理 》选项里的路由----------------------------------------------------
      // 任务列表
      {
        path: 'task',
        name: 'task',
        component: () => import("../views/admin/task/taskList.vue")
      },
      // 下面是《 intionList/信息列表 》选项里的路由----------------------------------------------------
      // 商品列表
      {
        path: 'Comty',
        name: 'Comty',
        component: () => import("../views/admin/intionList/ComtyView.vue")
      },
      // 订单列表
      {
        path: 'Order',
        name: 'Order',
        component: () => import("../views/admin/intionList/OrderView.vue")
      },


      // 如果没有自定义子路由，跳转大dash页面
      {
        path: "",
        redirect: "index"
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由权限信息
router.beforeEach((to, from, next) => {
  // 如果需要权限
  if (to.meta.requireAuth) {
    // 是否有token
    if (localStorage.getItem("token")) {
      // 有token就放行
      next()
    } else {
      // 如果没有跳转到首页，查新参数redirect是当前页的path
      Message({
        message: "需要先登录！",
        type: "warning"
      });
      next("/?redirect=" + to.path)
    }
  } else {
    // 如果没有权限直接放行
    next()
  }
})
export default router
